package com.foosun.core.util;

import java.io.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Created by Nelin on 14-3-2.
 */
public class FetchAPKIconUtils {

    private static String AAPT_PATH = "";

    static {
        AAPT_PATH = FetchAPKIconUtils.class.getResource("/tools").getPath();
    }

//    public static void main(String[] args) {
//        try {
//            getApkIconPath("C:\\Users\\Nelin\\Desktop\\training-platform\\AnZhi_5100.apk", "E:\\upload\\");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * @author Morton
     * apkUrl: apk路径
     * request: request
     */
    public static String getApkIconPath(String apkUrl, String savePath) throws IOException {
        byte b[] = new byte[1024];
        int length;
        ZipFile zipFile;
        String uuid = UUID.randomUUID().toString();//UUID构造文件名称，确保upload中文件名称无重复
        String logoUrl = savePath + uuid;

        String iconName = getIconName(apkUrl);
        zipFile = new ZipFile(new File(apkUrl));
        Enumeration enumeration = zipFile.entries();
        ZipEntry zipEntry = null;
        while (enumeration.hasMoreElements()) {
            zipEntry = (ZipEntry) enumeration.nextElement();
            if (zipEntry.isDirectory()) {

            } else {
                if (iconName.equals(zipEntry.getName())) {
                    OutputStream outputStream = new FileOutputStream(logoUrl);
                    InputStream inputStream = zipFile.getInputStream(zipEntry);
                    while ((length = inputStream.read(b)) > 0) {
                        outputStream.write(b, 0, length);
                    }
                    break;
                }
            }
        }
        return uuid;
    }

    /**
     * @author Morton
     * apkPath : apk路径
     * aaptPath : aapt路径
     */
    private static String getIconName(String apkPath) throws IOException {
        String apkOldName = "";
        Runtime rt = Runtime.getRuntime();
        Properties props = System.getProperties();
        String osName = props.getProperty("os.name");
        String order = AAPT_PATH + "/aapt" + " d badging " + apkPath + " | grep -E \"application:\"";
        if (osName.indexOf("Windows") > -1) {
            order = AAPT_PATH + "/aapt.exe" + " d badging \"" + apkPath + "\"";
        }
        System.err.println(order);
        System.err.println("----------------order------------------");

        Process proc = rt.exec(order);
        InputStream stDerr = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(stDerr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.contains("application:")) {
                apkOldName = line.substring(line.lastIndexOf("='") + 2, line.lastIndexOf("'")).trim().toLowerCase();
                break;
            }
        }
        return apkOldName;
    }
}
